package com.ys.web.mobao.util;

import com.alibaba.fastjson.JSONObject;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.spring.config.SystemError;
import com.ys.utils.spring.properties.Config;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class PersonalValid {
    public static boolean communication(String name,String idCard,String bankNo){
        String host = "http://yhkr.market.alicloudapi.com";
        String path = "/communication/personal/1886";
        String method = "POST";
        String appcode = Config.pp.get("personal.valid.appcode");
//        String appcode = "e8eda7db9a344340aae134991448c553";
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("acct_no", bankNo);
        bodys.put("idcard", idCard);
        bodys.put("name", name);

        boolean flag = false;
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            if(!jsonObject.getString("code").equals("10000")){
                SystemError.wrapBs("400",jsonObject.getString("message"));
            }
            JSONObject jsonObject2 = JSONObject.parseObject(jsonObject.getString("data"));
            flag = jsonObject2.getIntValue("state")==1;
        } catch (Exception e) {
            Logger.error("银行卡3要素验证异常",e);
        }
        return flag;
    }
}
